package com.shujia.JdbcUtil;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URL;


public class HdfsApi {
    FileSystem fs;
    @Before
    public void init()throws Exception{
        Configuration conf = new Configuration();
        conf.set("dfs.replication","1");

        URI uri = new URI("hdfs://master:9000");
         fs = FileSystem.get(uri, conf);
    }
    @Test
    public void mk() throws Exception {
        Path path = new Path("/Data/a/b");
        boolean mkdirs = fs.mkdirs(path);
        System.out.println(mkdirs);
    }
    @Test
    public void del() throws Exception {
        Path path = new Path("/Data/a/b");
        boolean b = fs.deleteOnExit(path);
        System.out.println(b);
    }
    @Test
    public void list() throws Exception {
        FileStatus[] fileStatuses = fs.listStatus(new Path("/Data"));
        for (FileStatus fileStatus : fileStatuses) {
            Path path = fileStatus.getPath();
            System.out.println(path);
            System.out.println(path.getName());
            long len = fileStatus.getLen();
            System.out.println(len);
            long blockSize = fileStatus.getBlockSize();
            System.out.println(blockSize);
        }
    }
    @Test
    public void put(){
        Path local = new Path("E:\\SJtest one day\\JavaMaven_Mysql\\spring_boot\\data\\cource.txt");
        Path hdfs = new Path("/Data/a");
        try {
            fs.copyFromLocalFile(local,hdfs);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//    @Test
//    public void get()throws Exception{
//        Path hdfs = new Path("Date/a/cource.txt");
//        Path local = new Path("E:\\SJtest one day\\JavaMaven_Mysql\\spring_boot\\Html\\cource.txt");
//        fs.copyToLocalFile(false,hdfs,local,true);
//    }



}
